﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Skyworth.CRM.Common;
using Skyworth.CRM.Service.IBLL;
using Skyworth.CRM.Service.Entity;

namespace Skyworth.CRM.Web.SystemManagement
{
    public partial class EditRole : BasePage
    {
        private IRoleBLL roleBll = (IRoleBLL)IocContainer.Instance.Resolve<IRoleBLL>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                if ("update" == Request.QueryString["Type"].ToString().ToLower())
                {
                    int roleId = 0;
                    int.TryParse(Request.QueryString["RoleId"], out roleId);
                    if(roleId>0)
                    {
                        Skyworth.CRM.Service.Entity.Roles roles = roleBll.GetRole(roleId);
                        this.txtRoleName.Text = roles.RoleName;
                        this.txtRoleDescription.Text = roles.RoleDescription;
                    }
                }
            }
        }

        protected void btnOk_Click(object sender, EventArgs e)
        {
            try
            {

                Boolean isOK = false;
                Skyworth.CRM.Service.Entity.Roles roles = new Skyworth.CRM.Service.Entity.Roles();
                roles.RoleName = txtRoleName.Text.Trim();
                roles.RoleDescription = txtRoleDescription.Text.Trim();

                if ("add" == Request.QueryString["Type"].ToString().ToLower())
                {
                    if (!roleBll.IsExisit(roles.RoleName, 0, false))
                    {
                        roleBll.Add(roles);
                        isOK = true;
                    }
                    else
                    {
                        Alert("该角色名称已经存在！");
                    }

                }
                else if ("update" == Request.QueryString["Type"].ToString().ToLower())
                {
                    int roleId = 0;
                    int.TryParse(Request.QueryString["RoleId"], out roleId);
                    roles.RoleId = roleId;
                    if (!roleBll.IsExisit(roles.RoleName, roleId, true))
                    {
                        roleBll.Update(roles);
                        isOK = true;
                    }
                    else
                    {
                        Alert("该角色名称已经存在！");
                    }
                }

                if (isOK)
                {
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "closeThisPage", "<script language=\"JavaScript\">parent.addWindow.close();parent.location.href = parent.location.href;</script>");
                }
            

            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
                Alert("发生异常：\r\n" + exception.Message);
            }
        }
    }
}
